package net.lwf.number.util;

import java.text.DecimalFormat;

import net.lwf.common.constant.ICommonConstant;

import org.apache.commons.lang.StringUtils;


/*
 * Possible patterns
 * 
 * Given value	Pattern				output after formatting
 * -----------	----------			-----------------------
 * 123456.789  	###,###.###  		123,456.789 
 * 123456.789  	###.##  			123456.79  
 * 123.78  		000000.000  		000123.780  
 * 12345.67  	$###,###.###  		$12,345.67  
 * 12345.67  	\u00A5###,###.###  	�12,345.67  
 * 
 */

public class NumberFormatter {
	
	//private static final String DEFAULT_FORMAT = "###";
	private static final String DEFAULT_FORMAT = "###,###";
	
	public static String format(String value) {
		return format(value, null);
	}	
	
	public static String format(String value, String format) {
		//Remove Commas in the value string if present
		value = StringUtils.remove(value, ICommonConstant.COMMA);
		
		double val = Double.parseDouble(value);
		if(format == null) {
			format = DEFAULT_FORMAT;
		}
		DecimalFormat formatter = new DecimalFormat(format);
		
		return formatter.format(val);
	}
	
	public static String optionalFormat(String value, String format) {
		if(!isNumber(value)) {
			return value;
		}
		return format(value, format);
	}	
	
	private static boolean isNumber(String value) {
		try {
			//int temp = Integer.parseInt(value);
			 Integer.parseInt(value);
			return true;
		}
		catch(Exception ex) {
			return false;
		}
	}
	
	public static String format(Double value, int decimaldigits, boolean padright) {
		String formatted = format(value, decimaldigits);
		if(padright) {
			int decimalPosition = formatted.indexOf(".");
			if(decimalPosition == -1 && decimaldigits != 0) {
				formatted = formatted + ".";
				for(int i=0; i<decimaldigits; i++) {
					formatted = formatted + "0";
				}
			}
			else if(decimalPosition != -1) {
				int formattedDecimalDigits = (formatted.substring(decimalPosition+1)).length();
				if(decimaldigits != formattedDecimalDigits) {
					//Padding needed
					int paddingDigits = decimaldigits - formattedDecimalDigits;
					for(int i=0; i<paddingDigits; i++) {
						formatted = formatted + "0";
					}
				}
			}
		}
		return formatted;
	}
	
	public static String format(String value, int decimaldigits) {
		return format(value, getFormat(decimaldigits));
	}
	
	public static String format(Double dbval, int decimaldigits) {
		double todouble = dbval.doubleValue();
		DecimalFormat formatter = new DecimalFormat(getFormat(decimaldigits));
		return formatter.format(todouble);
	}
	
	public static String getFormat(int decimaldigits) {
		String format = DEFAULT_FORMAT;
		if(decimaldigits != 0) {
			format = format + ".";
			for(int i=0; i<decimaldigits; i++) {
				format = format + "#";
			}
		}
		return format;
	}
	

//	public static String format(long value, String format) {
//		return format(new Long(value), format);
//	}
//	
//	public static String format(int value, String format) {
//		return format(new Integer(value), format);
//	}	
//	
//	public static String format(String value, String format) {
//		return format(value, format);
//	}	
//	
//	public static String format(double value, String format) {
//		return format(value, format);
//	}		
	
	public static void main(String[] args) {
		String value = "123456.99";
		//System.out.println("===> Converted number is: " + NumberFormatter.format(123.66, null));
		System.out.println("===> Converted number is: " + NumberFormatter.format(value, "###,###.###"));
		System.out.println("===> Converted number is: " + NumberFormatter.format(value, "$###,###.###"));
		System.out.println("===> Converted number is: " + NumberFormatter.format(value, "00000000.00"));
	}

} //NumberFormatter
